import 'package:flutter/material.dart';
import 'package:p_location/alarm/alarm_manage_tab.dart';
import 'package:p_location/alarm/constant/area_category_constant.dart';
import 'package:p_location/alarm/obj/job/obj_alarm_job_detail_info.dart';
import 'package:p_location/alarm/res/alarm_area_item.dart';
import 'package:p_location/alarm/res/alarm_vehicle_item.dart';
import 'package:p_location/alarm/ui/detail/area/page_alarm_area_person_detail.dart';
import 'package:p_location/alarm/ui/detail/area/page_alarm_area_person_lack_detail.dart';
import 'package:p_location/alarm/ui/detail/job/page_alarm_job_detail.dart';
import 'package:p_location/alarm/ui/detail/vehicle/page_alarm_vehicle_detail.dart';
import 'package:p_location/common/widget/look_all_view.dart';
import 'package:p_location/common/widget/no_result_widget.dart';
import 'package:p_location/common/widget/tag_view.dart';
import 'package:p_location/home/obj/obj_alarm_info.dart';
import 'package:p_location/map/res/search_person_detail.dart';
import 'package:p_location/utils/navigator_util.dart';

class HomeTabAlarmCardWidget extends StatefulWidget {
  int index = 0; //区域人员报警 0       作业报警  1  车辆报警 2
  int count = 0;
  List<ObjAlarmInfo> alarmInfoList = [];

  HomeTabAlarmCardWidget(this.index, this.count, this.alarmInfoList);

  @override
  _HomeTabAlarmCardWidgetState createState() => _HomeTabAlarmCardWidgetState();
}

class _HomeTabAlarmCardWidgetState extends State<HomeTabAlarmCardWidget> {
  @override
  void initState() {
    super.initState();
    print('count${widget.count}');
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {},
      child: Card(
        margin: EdgeInsets.only(left: 10, right: 10, bottom: 10),
        child: widget.count == 0
            ? NoResultWidget(
                'images/img_no_accident.png',
                "暂无数据",
                marginTop: 50,
                marginBottom: 50,
              )
            : Column(
                children: [
                  Container(
//        padding: EdgeInsets.all(15),
                    child: ListView.separated(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        separatorBuilder: (BuildContext context, int index) =>
                            Divider(height: 1.0),
                        itemCount: widget.count > 5 ? 5 : widget.count,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            onTap: () {
                              if (widget.index == 0) {
                                jumpAreaDetail(widget.alarmInfoList[index]);
                              } else if (widget.index == 1) {
                                jumpVehicleDetail(widget.alarmInfoList[index]);
                              } else if (widget.index == 2) {
                                jumpJobDetail(widget.alarmInfoList[index]);
                              }
                            },
                            child: Container(
                              padding: EdgeInsets.all(10),
                              child: Column(
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      TagView(
                                        widget
                                            .alarmInfoList[index].alarmTypeName,
                                        paddingLeft: 12,
                                        paddingBottom: 1,
                                        paddingRight: 12,
                                        paddingTop: 1,
                                        fillColor: Colors.white,
                                        strokeColor: Colors.red,
                                        textColor: Colors.red,
                                      ),
                                      Text(
                                        widget.alarmInfoList[index].alarmTime,
                                        style: TextStyle(
                                            color: Color.fromARGB(99, 0, 0, 0)),
                                      )
                                    ],
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(top: 5),
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                        widget.alarmInfoList[index].alarmInfo),
                                  )
                                ],
                              ),
                            ),
                          );
                        }),
                  ),
                  Container(
                      alignment: Alignment.center,
                      child: Column(
                        children: [
                          FractionallySizedBox(
                            widthFactor: 1,
                            child: Container(
                              height: 0.5,
                              color: Colors.grey[200],
                              child: Text("12"),
                            ),
                          ),
                          Offstage(
                            offstage: widget.count <= 5,
                            child: LookAllView(true, () {
                              NavigatorUtil.push(
                                  context,
                                  EmergencyManageTab(
                                    type: widget.index,
                                  ));
                            }),
                          )
                        ],
                      ))
                ],
              ),
      ),
    );
  }

  jumpAreaDetail(ObjAlarmInfo info) {
    AlarmAreaItem detail = AlarmAreaItem();
    detail.id = info.id;
    detail.sn = info.personSn;
    if (AreaCategoryConstant.list1.contains(info.alarmTypeName)) {
//      detail.sn = info.sn
      NavigatorUtil.push(
          context,
          PageAlarmAreaPersonDetail(detail, info.id, info.alarmTypeName,
              info.alarmInfo, info.alarmTime, info.status ?? "报警中"));
    } else if (AreaCategoryConstant.list2.contains(info.alarmTypeName)) {
      NavigatorUtil.push(
          context,
          PageAlarmAreaPersonLackDetail(
              detail,
              info.id,
              info.alarmTypeName == "缺员报警",
              info.alarmInfo,
              info.alarmTime,
              info.alarmTypeName,
              info.status ?? "报警中"));
    }
  }

  jumpVehicleDetail(ObjAlarmInfo info) {
    AlarmVehicleItem item = AlarmVehicleItem();
    item.id = info.id;
//    item.carNumber = info.personName;
    item.vehicleSn = info.vehicleSn;
    NavigatorUtil.push(
        context,
        PageAlarmVehicleDetail(item, info.id, info.alarmTypeName,
            info.alarmInfo, info.alarmTime, info.status ?? "报警中"));
  }

  jumpJobDetail(ObjAlarmInfo info) {
    AlarmJobDetailInfo alarmJobDetailInfo = AlarmJobDetailInfo();
    if (info != null) {
      alarmJobDetailInfo.alarmType = info.alarmTime;
      alarmJobDetailInfo.alarmTypeName = info.alarmTypeName;
      alarmJobDetailInfo.alarmInfo = info.alarmInfo;
      alarmJobDetailInfo.alarmTime = info.alarmTime;
      alarmJobDetailInfo.jobNumber = info.jobNumber ?? "";
      alarmJobDetailInfo.personName = info.personName ?? "";
      alarmJobDetailInfo.status = info.status ?? "报警中";
      alarmJobDetailInfo.personSn = info.personSn;
      alarmJobDetailInfo.jobStartTime = info.jobStartTime;
      alarmJobDetailInfo.jobEndTime = info.jobEndTime;
    }
    NavigatorUtil.push(context, PageAlarmJobDetail(alarmJobDetailInfo));
  }
}
